#include "functions.h"
void ball_pop_1(char map[][WIDTH], const int B1, const int B2, struct ball *ball2) //左边的球
{
    extern int ctrl_ball2;
    if (ctrl_ball2 < 1)
        ctrl_ball2 += GetKeyState(0x50) < 0;
    if (ctrl_ball2 > 0)
        prop_upd_1(map, B1, B2, ball2);
}

void ball_pop_2(char map[][WIDTH], const int B1, const int B2, struct ball *ball3) //右边的球
{
    extern int ctrl_ball3;
    if (ctrl_ball3 < 1)
        ctrl_ball3 += GetKeyState(0x60) < 0;
    if (ctrl_ball3 > 0)
        prop_upd_2(map, B1, B2, ball3);
}

void prop_upd_1(char map[][WIDTH], const int B1, const int B2, struct ball *b) //左边的球的更新
{
    extern int score[], ctrl_ball2;
    int y_next = (*b).pos[Y_POS] + (*b).dirc[Y_POS] * (*b).speed,
        x_next = (*b).pos[X_POS] + (*b).dirc[X_POS] * (*b).speed;
    if (x_next >= WIDTH - 1)
    {
        (*b).speed = 0;
        if (score[1] > 0) //防止右边的分数低于0
        {
            score[1]--;           
            return;
        }
    }
    if (x_next <= 0)
    {
        (*b).speed = 0; //停止运动

        return;
    }
    else if (x_next == 3)
    {
        if (y_next == B1)
        {
            (*b).speed *= 1.08;
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = 0;
        }
        else if (y_next == B1 + 1)
        {
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = 1;
        }
        else if (y_next == B1 - 1)
        {
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = -1;
        }
    }
    else if (x_next == WIDTH - 3)
    {
        if (y_next == B2)
        {
            (*b).speed *= 1.0;
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = 0;
        }
        else if (y_next == B2 + 1)
        {
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = 1;
        }
        else if (y_next == B2 - 1)
        {
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = -1;
        }
    }
    else if (y_next == 0 || y_next == HEIGHT - 1)
    {
        (*b).dirc[Y_POS] *= -1;
    }
    int ballx, bally;
    ballx = (int)((*b).pos[X_POS] += (*b).dirc[X_POS] * ((*b).speed));
    bally = (int)((*b).pos[Y_POS] += (*b).dirc[Y_POS] * ((*b).speed));
    map[bally][ballx] = 'O';
}

void prop_upd_2(char map[][WIDTH], const int B1, const int B2, struct ball *b) //右边的球的更新
{
    extern int score[], ctrl_ball3;
    int y_next = (*b).pos[Y_POS] + (*b).dirc[Y_POS] * (*b).speed,
        x_next = (*b).pos[X_POS] + (*b).dirc[X_POS] * (*b).speed;
    if (x_next >= WIDTH - 1)
    {
        (*b).speed = 0; //停止运动
        return;
    }
    if (x_next <= 0)
    {
        (*b).speed = 0;
        if (score[0] > 0)
        {
            score[0]--;          
            return;
        }
    }
    else if (x_next == 3)
    {
        if (y_next == B1)
        {
            (*b).speed *= 1.08;
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = 0;
        }
        else if (y_next == B1 + 1)
        {
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = 1;
        }
        else if (y_next == B1 - 1)
        {
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = -1;
        }
    }
    else if (x_next == WIDTH - 3)
    {
        if (y_next == B2)
        {
            (*b).speed *= 1.0;
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = 0;
        }
        else if (y_next == B2 + 1)
        {
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = 1;
        }
        else if (y_next == B2 - 1)
        {
            (*b).dirc[X_POS] *= -1;
            (*b).dirc[Y_POS] = -1;
        }
    }
    else if (y_next == 0 || y_next == HEIGHT - 1)
    {
        (*b).dirc[Y_POS] *= -1;
    }
    int ballx, bally;
    ballx = (int)((*b).pos[X_POS] += (*b).dirc[X_POS] * ((*b).speed));
    bally = (int)((*b).pos[Y_POS] += (*b).dirc[Y_POS] * ((*b).speed));
    map[bally][ballx] = 'Q';
}
